package com.crm.system.service.impl.callCenter;


import com.alibaba.fastjson.JSON;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.crm.common.model.DhPageInfo;
import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.common.util.CommonUtils;
import com.crm.model.entity.callCenter.CallForm;
import com.crm.model.entity.sys.FileUpload;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.qo.callCenter.CallFormQO;
import com.crm.model.qo.callCenter.CallMarkupQO;
import com.crm.model.vo.callCenter.CallFormDetailsVO;
import com.crm.model.vo.callCenter.CallFormVO;
import com.crm.model.vo.callCenter.CallReportVO;
import com.crm.model.vo.contactManager.ContactVO;
import com.crm.redis.mapper.ValueRMapper;
import com.crm.service.callCenter.CallFormDbService;
import com.crm.system.service.FileUploadService;
import com.crm.system.service.callCenter.CallFormService;
import com.crm.system.service.contactManager.ContactService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.net.URL;
import java.util.*;

/**
 * 呼叫中心-呼叫表单
 *
 * @Author renshaorong
 * @Date 2021/3/31
 */
@Slf4j
@Service
public class CallFormServiceImpl implements CallFormService {

    @Autowired
    private CallFormDbService callFormDbService;
    @Autowired
    private ContactService contactService;
    @Autowired
    private FileUploadService fileUploadService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private static final String endpoint = "https://oss-cn-qingdao.aliyuncs.com";
//        private static final String endpoint = "https://dcoss1.dcrm.cn";
    private static final String accessKeyId = "LTAI5tJAQ6jqvo3JkySW1Nwt";
    private static final String accessKeySecret = "itvmzbFWji33mIyrWvPmaCPiPqitYO";
    private static final String bucketName = "dc-ccoss";


    /**
     * 呼叫报表的key,上月
     */
    public static final String LAST_MONTH_DAY_KEY = "follow_report:LAST_MONTH_DAYS";
    public static final String LAST_MONTH_CALL_THINK_KEY = "call_report:last_month_call_think:CALL_THINK_";
    public static final String LAST_MONTH_CALL_NOT_THINK_KEY = "call_report:last_month_call_not_think:CALL_NOT_THINK_";
    public static final String LAST_MONTH_CALL_NOT_TAG_KEY = "call_report:last_month_call_not_tag:CALL_NOT_TAG_";
    public static final String LAST_MONTH_CALL_TAG_KEY = "call_report:last_month_call_tag:CALL_TAG_";
    public static final String LAST_MONTH_CALL_SPEAK_KEY = "call_report:last_month_call_speak:CALL_SPEAK_";
    public static final String LAST_MONTH_CALL_BAD_KEY = "call_report:last_month_call_bad:CALL_BAD_";
    public static final String LAST_MONTH_CALL_ANSWER_KEY = "call_report:last_month_call_answer:CALL_ANSWER_";
    public static final String LAST_MONTH_CALL_MISSED_KEY = "call_report:last_month_call_missed:CALL_MISSED_";
    public static final String LAST_MONTH_CALL_SUM_KEY = "call_report:last_month_call_sum:CALL_SUM_";
    public static final String LAST_MONTH_CALL_COMPARE_KEY = "call_report:last_month_call_compare:CALL_COMPARE_";

    /**
     * 呼叫报表的key,ben月
     */
    public static final String MONTH_DAY_KEY = "follow_report:MONTH_DAYS";
    public static final String MONTH_CALL_THINK_KEY = "call_report:month_call_think:CALL_THINK_";
    public static final String MONTH_CALL_NOT_THINK_KEY = "call_report:month_call_not_think:CALL_NOT_THINK_";
    public static final String MONTH_CALL_NOT_TAG_KEY = "call_report:month_call_not_tag:CALL_NOT_TAG_";
    public static final String MONTH_CALL_TAG_KEY = "call_report:month_call_tag:CALL_TAG_";
    public static final String MONTH_CALL_SPEAK_KEY = "call_report:month_call_speak:CALL_SPEAK_";
    public static final String MONTH_CALL_BAD_KEY = "call_report:month_call_bad:CALL_BAD_";
    public static final String MONTH_CALL_ANSWER_KEY = "call_report:month_call_answer:CALL_ANSWER_";
    public static final String MONTH_CALL_MISSED_KEY = "call_report:month_call_missed:CALL_MISSED_";
    public static final String MONTH_CALL_SUM_KEY = "call_report:month_call_sum:CALL_SUM_";
    public static final String MONTH_CALL_COMPARE_KEY = "call_report:month_call_compare:CALL_COMPARE_";
    /**
     * 呼叫表单列表
     *
     * @param pageIndex  页码
     * @param pageSize   分页大小
     * @param sortName   拍序列名字（暂时只支持创建时间排序）
     * @param sortOrder  排序规则(asc/desc)
     * @param callFormQO 查询条件
     * @return
     * @Author renshaorong
     * @Date 2021/4/1
     */
    @Override
    public DhPageInfo<CallFormVO> selectCallFormList(Integer pageIndex, Integer pageSize, CallFormQO callFormQO, String sortName, String sortOrder) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }

        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        boolean rs = CommonUtils.isAdmin(userAccount.getUserId());
        //单元测试时给默认值
//        boolean rs = CommonUtils.isAdmin("0");

        //增加删除状态查询条件(0:正常;1:已删除)
        callFormQO.setDeleteStatus((short) StatusCodeEnum.NORMAL_CODE.getState());
        Integer dateUnit = 0;
        if (!StringUtils.isBlank(callFormQO.getDateType())) {
            if ("week".equals(callFormQO.getDateType())) {
                //本周
                dateUnit = 1;
            }
            if ("month".equals(callFormQO.getDateType())) {
                //本月
                dateUnit = 2;
            }
            if ("today".equals(callFormQO.getDateType())) {
                //今天
                dateUnit = 3;
            }
        }

        StringBuilder order = new StringBuilder();
        if (!StringUtils.isBlank(sortName) && !StringUtils.isBlank(sortOrder)) {
            switch (sortName) {
                case "createTm": {
                    order.append("a.create_tm ").append(sortOrder);
                }
                break;
                default:
                    break;
            }
        }
        List<CallFormVO> callFormList = new ArrayList<CallFormVO>();
        if (callFormQO.getBs() == 0) {
            if (rs) {
                //超级管理员
                callFormList = callFormDbService.selectAllCallFormList(callFormQO, dateUnit, order.toString());
            } else {
                //普通管理员
                callFormQO.setCompanyId(userAccount.getCompanyId());
                //单元测试时给默认值
//                callFormQO.setCompanyId("9fac5144393311eb9e0a00163e08893d");
                callFormList = callFormDbService.selectCallFormListByCompanyId(callFormQO, dateUnit, order.toString());
            }
        } else {
            //普通用户
            callFormQO.setUserId(userAccount.getAccountId());
            //单元测试时给默认值
//            callFormQO.setUserId("71902468-8dee-4e30-b554-456d2140c7d9");
            callFormList = callFormDbService.selectCallFormListByUserId(callFormQO, dateUnit, order.toString());
        }

        PageInfo<CallFormVO> pageInfo = new PageInfo<CallFormVO>(callFormList);
        return new DhPageInfo<CallFormVO>(pageInfo, callFormList);
    }


    @Override
    public CallFormDetailsVO selectCallFormDetails(String contactId, String formId) {

        CallFormDetailsVO result = new CallFormDetailsVO();

        if (!StringUtils.isEmpty(contactId)){
            //获取联系人信息
            ContactVO contactVO = contactService.selectContactDetailById(contactId);
            if (Objects.nonNull(contactVO)){
                BeanUtils.copyProperties(contactVO, result);
                //获取头像
                FileUpload fileUpload = fileUploadService.selectFileUploadById(contactVO.getUploadId());
                if (Objects.nonNull(fileUpload)) {
                    result.setContactPhoto(fileUpload.getFilePath());
                }
            }
        }
        /*if (Objects.isNull(contactVO)) {
            return result;
        }*/
        //呼叫记录
        CallForm callForm = callFormDbService.selectCallFormByFormId(formId);

        if (Objects.nonNull(callForm)) {
            result.setFormId(callForm.getFormId());
            result.setCallRecord(getSecretUrl(callForm.getCallRecord()));
            result.setCallDuration(callForm.getCallDuration());
            result.setCreateTm(callForm.getCreateTm());
            result.setIsComplaintsCall(callForm.getIsComplaintsCall());
            result.setIsGoodCall(callForm.getIsGoodCall());
            result.setIsWarnCall(callForm.getIsWarnCall());
            result.setIsWordArt(callForm.getIsWordArt());
            result.setIsSpiteCall(callForm.getIsSpiteCall());
            result.setIsInterested(callForm.getIsInterested());
        }

        return result;
    }


    /**
     * @Description: 生成oss签名url
     * @Param: [objectName] 地址
     * @Return: java.lang.String
     * @Author: lixq-f
     * @Date: 2021/6/28
     **/
    @Override
    public String getSecretUrl(String objectName) {
        if (StringUtils.isEmpty(objectName)) {
            return null;
        }

        OSS ossClient = null;
        try {
            if (objectName.contains("/home/cic/cic2000/tone/RecordFile")) {
                objectName = objectName.replace("/home/cic/cic2000/tone/RecordFile", "RecFile");
            } else if (objectName.contains("http://118.190.175.96:8081/tone")) {
                objectName = objectName.replace("http://118.190.175.96:8081/tone", "RecFile");
            } else if (objectName.contains("http://47.104.218.1896:8081/Rec")) {
                objectName = objectName.replace("http://47.104.218.1896:8081/Rec", "RecFile");
            } else if (objectName.contains("http://47.104.218.189:8081/Rec")) {
                objectName = objectName.replace("http://47.104.218.189:8081/Rec", "RecFile");
            } else {
                return objectName;
            }
            // 创建OSSClient实例。
            ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            // 设置URL过期时间为1小时。
            Date expiration = new Date(new Date().getTime() + 3600 * 1000);
            // 生成签名URL。
            URL url = ossClient.generatePresignedUrl(bucketName, objectName, expiration);
            log.info("getSecretUrl:{}", JSON.toJSONString(url));
            return url.toString();
        } catch (Exception e) {
            log.error("getSecretUrl error :{}", e);
        } finally {
            // 关闭OSSClient。
            if (null != ossClient){
                ossClient.shutdown();
            }
        }

        return null;
    }


    public static void main(String[] args) {
//        String callUrl = "RecFile/20210628/05300009157_91011_1400_20210628142529.wav";
        String callUrl = "http://47.104.218.1896:8081/Rec/20210723/15600000603_91008_1399_20210723083754.wav";
//        String callUrl = "RecFile/20210629/91010_13963834552_1_20210629092923.wav";
        CallFormServiceImpl impl = new CallFormServiceImpl();
        String secretUrl = impl.getSecretUrl(callUrl);
        System.out.println(secretUrl);

    }


    @Override
    public DhPageInfo<CallFormVO> selectCallDetailsList(Integer pageIndex, Integer pageSize, CallFormQO callFormQO, String sortName, String sortOrder) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }

        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        boolean rs = CommonUtils.isAdmin(userAccount.getUserId());
        //单元测试时给默认值
//        boolean rs = CommonUtils.isAdmin("0");

        //增加删除状态查询条件(0:正常;1:已删除)
        callFormQO.setDeleteStatus((short) StatusCodeEnum.NORMAL_CODE.getState());
        Integer dateUnit = 0;
        if (!StringUtils.isBlank(callFormQO.getDateType())) {
            if ("week".equals(callFormQO.getDateType())) {
                //本周
                dateUnit = 1;
            }
            if ("month".equals(callFormQO.getDateType())) {
                //本月
                dateUnit = 2;
            }
            if ("today".equals(callFormQO.getDateType())) {
                //今天
                dateUnit = 3;
            }
        }

        StringBuilder order = new StringBuilder();
        if (!StringUtils.isBlank(sortName) && !StringUtils.isBlank(sortOrder)) {
            switch (sortName) {
                case "createTm": {
                    order.append("a.create_tm ").append(sortOrder);
                }
                break;
                case "callDuration": {
                    order.append("a.call_duration ").append(sortOrder);
                }
                break;
                default:
                    break;
            }
        }
        List<CallFormVO> callFormList = new ArrayList<CallFormVO>();
        if (callFormQO.getBs() == 0) {
            if (rs) {
                //超级管理员
                callFormList = callFormDbService.selectAllCallDetailsList(callFormQO, dateUnit, order.toString());
            } else {
                //普通管理员
                callFormQO.setCompanyId(userAccount.getCompanyId());
                //单元测试时给默认值
//                callFormQO.setCompanyId("9fac5144393311eb9e0a00163e08893d");
                callFormList = callFormDbService.selectCallDetailsListByCompanyId(callFormQO, dateUnit, order.toString());
            }
        } else {
            //普通用户
            callFormQO.setUserId(userAccount.getAccountId());
            //单元测试时给默认值
//            callFormQO.setUserId("71902468-8dee-4e30-b554-456d2140c7d9");
            callFormList = callFormDbService.selectCallDetailsListByUserId(callFormQO, dateUnit, order.toString());
        }
        for (CallFormVO callFormVO : callFormList) {
            callFormVO.setCallRecord(getSecretUrl(callFormVO.getCallRecord()));
        }

        PageInfo<CallFormVO> pageInfo = new PageInfo<CallFormVO>(callFormList);
        return new DhPageInfo<CallFormVO>(pageInfo, callFormList);
    }

    /**
     * 折线图（接听、未接听、接听总数三条折线）
     *
     * @param dateUnit 上月/本周/本月/本季度/本年
     * @return
     * @Author renshaorong
     * @Date 2021/4/16
     */
    @Override
    public Map<String, Object> getPolyLineByAnswers(Integer dateUnit) {
        String userAccountId = getUserAccountId();

        try {
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("type", this.getPolyLineTypeList());//折线类型

            switch (dateUnit){
                case 1 : { //上月

                    List<String> lastMonthDay = ValueRMapper.getRedisObjectList(stringRedisTemplate, LAST_MONTH_DAY_KEY,String.class);
                    if(lastMonthDay == null){ // redis中无数据，抛出异常，从数据库中读取数据
                        log.error("key为"+LAST_MONTH_DAY_KEY+"的值不存在");
                        throw new Exception();
                    }

                    //接听
                    List<String> answer = ValueRMapper.getRedisObjectList(stringRedisTemplate, LAST_MONTH_CALL_ANSWER_KEY+userAccountId,String.class);
                    if(answer == null){ // redis中无数据，抛出异常，从数据库中读取数据
                        log.error("key为"+LAST_MONTH_CALL_ANSWER_KEY+userAccountId+"的值不存在");
                        throw new Exception();
                    }

                    //未接听
                    List<String> missed = ValueRMapper.getRedisObjectList(stringRedisTemplate, LAST_MONTH_CALL_MISSED_KEY+userAccountId,String.class);
                    if(missed == null){ // redis中无数据，抛出异常，从数据库中读取数据
                        log.error("key为"+LAST_MONTH_CALL_MISSED_KEY+userAccountId+"的值不存在");
                        throw new Exception();
                    }

                    //接听总数
                    List<String> sum = ValueRMapper.getRedisObjectList(stringRedisTemplate, LAST_MONTH_CALL_SUM_KEY+userAccountId,String.class);
                    if(sum == null){ // redis中无数据，抛出异常，从数据库中读取数据
                        log.error("key为"+LAST_MONTH_CALL_SUM_KEY+userAccountId+"的值不存在");
                        throw new Exception();
                    }

                    resultMap.put("time", lastMonthDay);//时间轴
                    resultMap.put("answer", answer);//接听
                    resultMap.put("missed", missed);//未接听
                    resultMap.put("sum", sum);//接听总数

                    return resultMap;
                }
                case 3 : { //本月
                    List<String> lastMonthDay = ValueRMapper.getRedisObjectList(stringRedisTemplate, MONTH_DAY_KEY,String.class);
                    if(lastMonthDay == null){ // redis中无数据，抛出异常，从数据库中读取数据
                        log.error("key为"+MONTH_DAY_KEY+"的值不存在");
                        throw new Exception();
                    }

                    //接听
                    List<String> answer = ValueRMapper.getRedisObjectList(stringRedisTemplate, MONTH_CALL_ANSWER_KEY+userAccountId,String.class);
                    if(answer == null){ // redis中无数据，抛出异常，从数据库中读取数据
                        log.error("key为"+MONTH_CALL_ANSWER_KEY+userAccountId+"的值不存在");
                        throw new Exception();
                    }

                    //未接听
                    List<String> missed = ValueRMapper.getRedisObjectList(stringRedisTemplate, MONTH_CALL_MISSED_KEY+userAccountId,String.class);
                    if(missed == null){ // redis中无数据，抛出异常，从数据库中读取数据
                        log.error("key为"+MONTH_CALL_MISSED_KEY+userAccountId+"的值不存在");
                        throw new Exception();
                    }

                    //接听总数
                    List<String> sum = ValueRMapper.getRedisObjectList(stringRedisTemplate, MONTH_CALL_SUM_KEY+userAccountId,String.class);
                    if(sum == null){ // redis中无数据，抛出异常，从数据库中读取数据
                        log.error("key为"+MONTH_CALL_SUM_KEY+userAccountId+"的值不存在");
                        throw new Exception();
                    }

                    resultMap.put("time", lastMonthDay);//时间轴
                    resultMap.put("answer", answer);//接听
                    resultMap.put("missed", missed);//未接听
                    resultMap.put("sum", sum);//接听总数

                    return resultMap;
                }
                default: throw new Exception();
            }
        }catch (Exception e){
            log.error("呼叫报表中getPolyLineByAnswers方法捕获异常，此处数据从数据库中读取，请检查异常");
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("time", callFormDbService.getTimeList(dateUnit));//时间轴
            resultMap.put("type", this.getPolyLineTypeList());//折线类型
            //接听、未接听、接听总数
            resultMap.put("answer", callFormDbService.getPolyLineByAnswers(dateUnit, 1, userAccountId));//接听
            resultMap.put("missed", callFormDbService.getPolyLineByAnswers(dateUnit, 2, userAccountId));//未接听
            resultMap.put("sum", callFormDbService.getPolyLineByAnswers(dateUnit, 3, userAccountId));//接听总数
            return resultMap;
        }
    }

    @Override
    public boolean updateLabel(String formId, Integer label) {
        return callFormDbService.updateLabel(formId, label);
    }

    /**
     * 获取跟进类型List（用于折线图的多条折线）
     *
     * @return
     * @Author renshaorong
     * @Date 2021/4/13
     */
    public List<String> getPolyLineTypeList() {
        List<String> polyLineTypeList = new ArrayList<>();
        polyLineTypeList.add("接听");
        polyLineTypeList.add("未接听");
        polyLineTypeList.add("接听总数");
        return polyLineTypeList;
    }

    /**
     * 饼状图（30s/60s/90s/大于90s）
     *
     * @param dateUnit 上月/本周/本月/本季度/本年,redis中仅存上月、本月
     * @return
     * @Author renshaorong
     * @Date 2021/4/16
     * @add 结果新增较上月数据
     */
    @Override
    public List<Map> getMinutePieData(Integer dateUnit) {
        String userAccountId = this.getUserAccountId();
//        return callFormDbService.getMinutePieData(dateUnit, userAccountId);
        try {
            switch (dateUnit){
                case 1 : { //上月
                    List<Map> redisObject = ValueRMapper.getRedisObjectList(stringRedisTemplate, LAST_MONTH_CALL_COMPARE_KEY+userAccountId, Map.class);
                    if(redisObject == null){ // redis中无数据，抛出异常，从数据库中读取数据
                        log.error("key为"+LAST_MONTH_CALL_COMPARE_KEY+userAccountId+"的值不存在");
                        throw new Exception();
                    }
                    return redisObject;
                }
                case 3 : { //本月
                    List<Map> redisObject = ValueRMapper.getRedisObjectList(stringRedisTemplate, MONTH_CALL_COMPARE_KEY+userAccountId, Map.class);
                    if(redisObject == null){
                        log.error("key为"+MONTH_CALL_COMPARE_KEY+userAccountId+"的值不存在");
                        throw new Exception();
                    }
                    return redisObject;
                }
                default: return callFormDbService.getMinutePieData(dateUnit, userAccountId); //除 上月 本月 数据外，其余需走数据库查询
            }
        }catch (Exception e){
            log.error("呼叫报表中getMinutePieData方法捕获异常，此处数据从数据库中读取，请检查异常");
            return callFormDbService.getMinutePieData(dateUnit, userAccountId);
        }
    }

    /**
     * 统计表（标签：1 意向电话 2 无意向电话 3 未标记电话 4 抽取标签电话 5 产生话术电话 6 恶意回复电话）
     *
     * @param dateUnit
     * @return
     * @Author renshaorong
     * @Date 2021/4/16
     */
    @Override
    public List<CallReportVO> selectcallReportVOList(Integer dateUnit) {
        String userAccountId = getUserAccountId();
        try {
            List<CallReportVO> resultList = new ArrayList<>();
            switch (dateUnit){
                case 1 : { //上月
                    // 1、意向电话
                    CallReportVO callThink = ValueRMapper.getRedisObject(stringRedisTemplate, LAST_MONTH_CALL_THINK_KEY+userAccountId, CallReportVO.class);
                    if(callThink == null){ // redis中无数据，抛出异常，从数据库中读取数据
                        log.error("key为"+LAST_MONTH_CALL_THINK_KEY+userAccountId+"的值不存在");
                        throw new Exception();
                    }
                    resultList.add(callThink);

                    // 2、无意向电话
                    CallReportVO callNotThink = ValueRMapper.getRedisObject(stringRedisTemplate, LAST_MONTH_CALL_NOT_THINK_KEY+userAccountId, CallReportVO.class);
                    if(callNotThink == null){ // redis中无数据，抛出异常，从数据库中读取数据
                        log.error("key为"+LAST_MONTH_CALL_NOT_THINK_KEY+userAccountId+"的值不存在");
                        throw new Exception();
                    }
                    resultList.add(callNotThink);

                    // 3、未标记电话
                    CallReportVO callNotTag = ValueRMapper.getRedisObject(stringRedisTemplate, LAST_MONTH_CALL_NOT_TAG_KEY+userAccountId, CallReportVO.class);
                    if(callNotTag == null){ // redis中无数据，抛出异常，从数据库中读取数据
                        log.error("key为"+LAST_MONTH_CALL_NOT_TAG_KEY+userAccountId+"的值不存在");
                        throw new Exception();
                    }
                    resultList.add(callNotTag);

                    // 4、抽取标签电话
                    CallReportVO callTag = ValueRMapper.getRedisObject(stringRedisTemplate, LAST_MONTH_CALL_TAG_KEY+userAccountId, CallReportVO.class);
                    if(callTag == null){ // redis中无数据，抛出异常，从数据库中读取数据
                        log.error("key为"+LAST_MONTH_CALL_TAG_KEY+userAccountId+"的值不存在");
                        throw new Exception();
                    }
                    resultList.add(callTag);

                    // 5、产生话术电话
                    CallReportVO callSpeak = ValueRMapper.getRedisObject(stringRedisTemplate, LAST_MONTH_CALL_SPEAK_KEY+userAccountId, CallReportVO.class);
                    if(callSpeak == null){ // redis中无数据，抛出异常，从数据库中读取数据
                        log.error("key为"+LAST_MONTH_CALL_SPEAK_KEY+userAccountId+"的值不存在");
                        throw new Exception();
                    }
                    resultList.add(callSpeak);

                    // 6、恶意回复电话
                    CallReportVO callBad = ValueRMapper.getRedisObject(stringRedisTemplate, LAST_MONTH_CALL_BAD_KEY+userAccountId, CallReportVO.class);
                    if(callBad == null){ // redis中无数据，抛出异常，从数据库中读取数据
                        log.error("key为"+LAST_MONTH_CALL_BAD_KEY+userAccountId+"的值不存在");
                        throw new Exception();
                    }
                    resultList.add(callBad);

                    return resultList;
                }
                case 3 : { //本月
                    // 1、意向电话
                    CallReportVO callThink = ValueRMapper.getRedisObject(stringRedisTemplate, MONTH_CALL_THINK_KEY+userAccountId, CallReportVO.class);
                    if(callThink == null){ // redis中无数据，抛出异常，从数据库中读取数据
                        log.error("key为"+MONTH_CALL_THINK_KEY+userAccountId+"的值不存在");
                        throw new Exception();
                    }
                    resultList.add(callThink);

                    // 2、无意向电话
                    CallReportVO callNotThink = ValueRMapper.getRedisObject(stringRedisTemplate, MONTH_CALL_NOT_THINK_KEY+userAccountId, CallReportVO.class);
                    if(callNotThink == null){ // redis中无数据，抛出异常，从数据库中读取数据
                        log.error("key为"+MONTH_CALL_NOT_THINK_KEY+userAccountId+"的值不存在");
                        throw new Exception();
                    }
                    resultList.add(callNotThink);

                    // 3、未标记电话
                    CallReportVO callNotTag = ValueRMapper.getRedisObject(stringRedisTemplate, MONTH_CALL_NOT_TAG_KEY+userAccountId, CallReportVO.class);
                    if(callNotTag == null){ // redis中无数据，抛出异常，从数据库中读取数据
                        log.error("key为"+MONTH_CALL_NOT_TAG_KEY+userAccountId+"的值不存在");
                        throw new Exception();
                    }
                    resultList.add(callNotTag);

                    // 4、抽取标签电话
                    CallReportVO callTag = ValueRMapper.getRedisObject(stringRedisTemplate, MONTH_CALL_TAG_KEY+userAccountId, CallReportVO.class);
                    if(callTag == null){ // redis中无数据，抛出异常，从数据库中读取数据
                        log.error("key为"+MONTH_CALL_TAG_KEY+userAccountId+"的值不存在");
                        throw new Exception();
                    }
                    resultList.add(callTag);

                    // 5、产生话术电话
                    CallReportVO callSpeak = ValueRMapper.getRedisObject(stringRedisTemplate, MONTH_CALL_SPEAK_KEY+userAccountId, CallReportVO.class);
                    if(callSpeak == null){ // redis中无数据，抛出异常，从数据库中读取数据
                        log.error("key为"+MONTH_CALL_SPEAK_KEY+userAccountId+"的值不存在");
                        throw new Exception();
                    }
                    resultList.add(callSpeak);

                    // 6、恶意回复电话
                    CallReportVO callBad = ValueRMapper.getRedisObject(stringRedisTemplate, MONTH_CALL_BAD_KEY+userAccountId, CallReportVO.class);
                    if(callBad == null){ // redis中无数据，抛出异常，从数据库中读取数据
                        log.error("key为"+MONTH_CALL_BAD_KEY+userAccountId+"的值不存在");
                        throw new Exception();
                    }
                    resultList.add(callBad);

                    return resultList;
                }
                default: throw new Exception(); //除 上月 本月 数据外，其余需走数据库查询
            }
        }catch (Exception e){
            log.error("呼叫报表中 selectcallReportVOList 方法捕获异常，此处数据从数据库中读取，请检查异常");
            List<CallReportVO> resultList = new ArrayList<>();
            resultList.add(callFormDbService.selectcallReportVOList(dateUnit, userAccountId, 1));
            resultList.add(callFormDbService.selectcallReportVOList(dateUnit, userAccountId, 2));
            resultList.add(callFormDbService.selectcallReportVOList(dateUnit, userAccountId, 3));
            resultList.add(callFormDbService.selectcallReportVOList(dateUnit, userAccountId, 4));
            resultList.add(callFormDbService.selectcallReportVOList(dateUnit, userAccountId, 5));
            resultList.add(callFormDbService.selectcallReportVOList(dateUnit, userAccountId, 6));
            return resultList;
        }
    }

    /**
     * 获取当前登陆用户的id
     *
     * @return
     * @Author renshaorong
     * @Date 2021/4/15
     */
    public String getUserAccountId() {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        return userAccount.getAccountId();
    }


    /**
     * @Description: 呼叫表单标记
     * @Param: [markupQO]
     * @Return: boolean
     * @Author: lixq-f
     * @Date: 2021/5/28
     **/
    @Override
    public boolean callMarkup(CallMarkupQO markupQO) {
        return callFormDbService.callMarkup(markupQO);
    }


    /**
     * @Description: 根据表单ID获取表单记录
     * @Param: [formId]
     * @Return: com.crm.model.entity.callCenter.CallForm
     * @Author: lixq-f
     * @Date: 2021/5/28
     **/
    @Override
    public CallForm selectCallFormByFormId(String formId) {
        CallForm callForm = callFormDbService.selectCallFormByFormId(formId);
        if (Objects.isNull(callForm)) {
            callForm = new CallForm();
        }
        return callForm;
    }


    /**
     * @Description: 修改呼叫表单联系人
     * @Param: [callFormQO]
     * @Return: boolean
     * @Author: lixq-f
     * @Date: 2021/5/31
     **/
    @Override
    public boolean updateContact(CallFormQO callFormQO) {
        return callFormDbService.updateContact(callFormQO);
    }


}
